import { createApp } from "vue";
import Message from "@/components/Message.vue";

// 无法从vue组件直接导入
export type MessageType = "success" | "error" | "default";

const createMessage = (message: string, type: MessageType, timeout = 2000) => {
  const messageInstance = createApp(Message, {
    message,
    type,
  });
  const mountNode = document.createElement("div");
  document.body.appendChild(mountNode);
  messageInstance.mount(mountNode);
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      messageInstance.unmount();
      document.body.removeChild(mountNode);
      resolve(true);
    }, timeout);
  });
};

export default createMessage;
